using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Serialization;

namespace OverSizeImage
{
    #region 转换器
    public class ShapeEnumVisitConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ShapeEnum status = (ShapeEnum)value;
            return status == (ShapeEnum)Enum.Parse(typeof(ShapeEnum), parameter.ToString()) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility visit = (Visibility)value;
            if (visit == Visibility.Visible)
                return (ShapeEnum)Enum.Parse(typeof(ShapeEnum), parameter.ToString());
            else
                return null;
        }
    }

    public class StatusEnumGroupConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ImageStatusEnum status = (ImageStatusEnum)value;
            return status == (ImageStatusEnum)Enum.Parse(typeof(ImageStatusEnum), parameter.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool isChecked = (bool)value;
            if (isChecked)
                return (ImageStatusEnum)Enum.Parse(typeof(ImageStatusEnum), parameter.ToString());
            else
                return null;
        }
    }

    public class NumDivideConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return double.Parse(parameter.ToString()) / (double)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

    public class TrueToVisitConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value == true ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }


    public class ByteArrayToImageConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            BitmapImage imageSource = null;
            byte[] data = (byte[])value;
            if (data != null)
            {
                imageSource = new BitmapImage();
                imageSource.BeginInit();
                imageSource.StreamSource = new MemoryStream(data);
                imageSource.EndInit();
            }
            return imageSource;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
    
    public class FalseToVisitConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value == true ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
    #endregion

    //public class RectSelectedEventArgs : RoutedEventArgs
    //{
    //    public RectSelectedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }
    //}

    public enum ImageStatusEnum
    {
        概览,
        浏览,
        编辑,
        选点
    }

    public enum ImageLoadModelEnum
    {
        正常,
        瓦片,
        位图
    }

    public enum ShapeEnum
    {
        矩形,
        圆形,
        多边形,
        线段
    }

    public class BackWorkTileModel
    {
        public long Key = Guid.NewGuid().GetHashCode();
        public int DecodeWidth { get; set; }
        public int DecodeHeight { get; set; }
        public int ScreenWidth { get; set; }
        public int ScreenHeight { get; set; }
        public int StartX { get; set; }
        public int StartY { get; set; }
        public int CutWidth { get; set; }
        public int CutHeight { get; set; }
        public Point RefushPoint { get; set; }
    }

    public class ElementBaseModel : ImageShowBaseModel
    {
        private ShapeEnum shape = ShapeEnum.矩形;
        public ShapeEnum Shape
        {
            get { return shape; }
            set { shape = value; RaisePropertyChanged(); }
        }

        private bool isSelected = false;
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; RaisePropertyChanged(); }
        }
        private string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }

        private double x = 0;
        /// <summary>
        /// X坐标
        /// </summary>
        public double X
        {
            get { return x; }
            set { x = value; RaisePropertyChanged(); }
        }

        private double y = 0;
        /// <summary>
        /// Y坐标
        /// </summary>
        public double Y
        {
            get { return y; }
            set { y = value; RaisePropertyChanged(); }
        }

        private double width = 0;
        /// <summary>
        /// 宽
        /// </summary>
        public double Width
        {
            get { return width; }
            set { width = value; RaisePropertyChanged(); }
        }

        private double height = 0;
        /// <summary>
        /// 高
        /// </summary>
        public double Height
        {
            get { return height; }
            set { height = value; RaisePropertyChanged(); }
        }

        private double angle = 0.0;
        public double Angle
        {
            get { return angle; }
            set { angle = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<Point> points = new ObservableCollection<Point>();
        public ObservableCollection<Point> Points
        {
            get { return points; }
            set { points = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<PathModel> pathList = new ObservableCollection<PathModel>();
        public ObservableCollection<PathModel> PathList
        {
            get { return pathList; }
            set { pathList = value; RaisePropertyChanged(); }
        }

        [XmlIgnore]
        /// <summary>
        /// 边框颜色
        /// </summary>
        public Brush RectBrush
        {
            get { return new SolidColorBrush((Color)ColorConverter.ConvertFromString(BrushString)); }
        }

        private string brushString = "#37CF74";
        /// <summary>
        /// 边框颜色
        /// </summary>
        public string BrushString
        {
            get { return brushString; }
            set { brushString = value; RaisePropertyChanged(); }
        }

        private bool dragEnabled = true;
        /// <summary>
        /// 拖动使能
        /// </summary>
        public bool DragEnabled
        {
            get { return dragEnabled; }
            set { dragEnabled = value; RaisePropertyChanged(); }
        }

        private byte[] imageData = null;
        public byte[] ImageData
        {
            get { return imageData; }
            set { imageData = value; RaisePropertyChanged(); }
        }

        private Visibility visit = Visibility.Visible;
        public Visibility Visit
        {
            get { return visit; }
            set { visit = value; RaisePropertyChanged(); }
        }

    }

    public class PathModel : ImageShowBaseModel
    {
        private ObservableCollection<Point> points = new ObservableCollection<Point>();
        public ObservableCollection<Point> Points
        {
            get { return points; }
            set { points = value; RaisePropertyChanged(); }
        }

        private int result = 0;
        /// <summary>
        /// 结果
        /// </summary>
        public int Result
        {
            get { return result; }
            set { result = value; RaisePropertyChanged(); }
        }
    }

    public class ImageShowBaseModel : ViewModelBase
    {
        private int order = 0;
        public int Order
        {
            get { return order; }
            set { order = value; RaisePropertyChanged(); }
        }

        private long key = Guid.NewGuid().GetHashCode();
        /// <summary>
        /// Key
        /// </summary>
        public long Key
        {
            get { return key; }
            set { key = value; RaisePropertyChanged(); }
        }
    }

    public class VisualHost_IS : FrameworkElement
    {
        Visual child;

        public VisualHost_IS(Visual child) 
        {
            if (child == null) throw new ArgumentNullException("child");
            this.child = child;
            AddVisualChild(child);
        }

        protected override Visual GetVisualChild(int index)
        {
            return (index == 0) ? child : null;
        }

        protected override int VisualChildrenCount
        {
            get { return 1; }
        }
    }
}
